package org.chunchu.todo.mvp.impl;

import javax.inject.Inject;

import org.chunchu.todo.Main;
import org.chunchu.todo.dao.TaskDao;
import org.chunchu.todo.model.Task;
import org.mvp.AbstractPresenter;

import roboguice.inject.InjectExtra;

public class AddTaskPresenter extends AbstractPresenter<AddTask> {

	@Inject
	TaskDao taskDao;

	@InjectExtra(AddTask.EXTRA_TASK)
	Task extraTask;

	@Override
	public void initializeWith(AddTask activity) {
		super.initializeWith(activity);
		if (extraTask != null) {
			activity.setTask(extraTask);
		}
	}

	public void saveTask(Task task) {
		extraTask.setName(task.getName());
		extraTask.setDetail(task.getDetail());
		if (!taskDao.isValid(extraTask))
			return;

		String action = getActivity().getAction();
		if (Main.ACTION_ADD.equals(action)) {
			taskDao.add(extraTask);
		} else if (Main.ACTION_EDIT.equals(action)) {
			extraTask.setName(task.getName());
			extraTask.setDetail(task.getDetail());
			// extraTask.setCategoryId(task.getCategoryId());
			taskDao.update(extraTask);
		}

		getActivity().goToMain();
	}

	public void cancel() {

	}

}
